package com.hejia.bulbul.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.hejia.alauda.entity.BankInfo;
import com.hejia.alauda.entity.PayGate;
import com.hejia.alauda.entity.PayGateBank;
import com.hejia.alauda.service.PaymentService;
import com.hejia.alauda.utils.Pager;

@Controller
@RequestMapping("bankmanage")
public class BankManageController extends BaseController {

	@Resource
	private PaymentService bankInfoService;

	@Resource
	private PaymentService payGateService;

	@Resource
	private PaymentService payGateBankService;

	@RequestMapping(value = "bankmanagelist", method = RequestMethod.GET)
	public String bankmanageList(ModelMap model, Pager<BankInfo> pager) {
		Pager<BankInfo> bankmanage = bankInfoService.findAllBankInfo(pager);

		List<Map<String, Object>> paygateList = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < bankmanage.getList().size(); i++) {
			Long webPayGateid = (long) 0;
			Long h5PayGateid = (long) 0;
			Long iosPayGateid = (long) 0;
			Long androidPayGateid = (long) 0;
			String webPayGateName = "无";
			String h5PayGateName = "无";
			String iosPayGateName = "无";
			String androidPayGateName = "无";

			Map<String, Object> paygateMap = new HashMap<String, Object>();
			if (bankmanage.getList().get(i).getWebPaygate() != null) {
				webPayGateid = bankmanage.getList().get(i).getWebPaygate()
						.getId();
				webPayGateName = bankmanage.getList().get(i).getWebPaygate()
						.getGateName();
			}
			if (bankmanage.getList().get(i).getH5Paygate() != null) {
				h5PayGateid = bankmanage.getList().get(i).getH5Paygate()
						.getId();
				h5PayGateName = bankmanage.getList().get(i).getH5Paygate()
						.getGateName();
			}
			if (bankmanage.getList().get(i).getiOSPaygate() != null) {
				iosPayGateid = bankmanage.getList().get(i).getiOSPaygate()
						.getId();
				iosPayGateName = bankmanage.getList().get(i).getiOSPaygate()
						.getGateName();
			}
			if (bankmanage.getList().get(i).getAndroidPaygate() != null) {
				androidPayGateid = bankmanage.getList().get(i)
						.getAndroidPaygate().getId();
				androidPayGateName = bankmanage.getList().get(i)
						.getAndroidPaygate().getGateName();
			}

			PayGateBank webPayGateBank = payGateBankService
					.getPayGateBankByPayGateIdAndBankcode(webPayGateid,
							bankmanage.getList().get(i).getBankCode());
			PayGateBank h5PayGateBank = payGateBankService
					.getPayGateBankByPayGateIdAndBankcode(h5PayGateid,
							bankmanage.getList().get(i).getBankCode());
			PayGateBank iosPayGateBank = payGateBankService
					.getPayGateBankByPayGateIdAndBankcode(iosPayGateid,
							bankmanage.getList().get(i).getBankCode());
			PayGateBank androidPayGateBank = payGateBankService
					.getPayGateBankByPayGateIdAndBankcode(androidPayGateid,
							bankmanage.getList().get(i).getBankCode());

			paygateMap.put("bankmanageid", bankmanage.getList().get(i).getId());
			paygateMap.put("bankname", bankmanage.getList().get(i)
					.getBankName());
			paygateMap.put("bankcode", bankmanage.getList().get(i)
					.getBankCode());
			paygateMap.put("bindable", bankmanage.getList().get(i)
					.getBindable());

			paygateMap.put("webPayGate", webPayGateName);
			paygateMap.put("h5PayGate", h5PayGateName);
			paygateMap.put("iosPayGate", iosPayGateName);
			paygateMap.put("androidPayGate", androidPayGateName);

			paygateMap.put("webPayGateBank", webPayGateBank);
			paygateMap.put("h5PayGateBank", h5PayGateBank);
			paygateMap.put("iosPayGateBank", iosPayGateBank);
			paygateMap.put("androidPayGateBank", androidPayGateBank);
			paygateList.add(paygateMap);

		}

		model.put("paygateList", paygateList);

		return "/setting/bankmanage_list";
	}

	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "bankmanageadd", method = RequestMethod.GET)
	public String addBankManage(ModelMap model) {
		List<PayGate> pgList = payGateService.getPayGateList();
		model.put("pgList", pgList);
		return "/setting/bankmanage_input";
	}

	/**
	 * 添加完保存
	 */
	@RequestMapping(value = "bankmanageadd", method = RequestMethod.POST)
	public String saveBankManage(BankInfo bankInfo, ModelMap model,
			Long webpaygateid, Long h5paygateid, Long iospaygateid,
			Long androidpaygateid, RedirectAttributes redirectAttributes) {
		PayGate webpayGate = payGateService.getPayGateById(webpaygateid);
		PayGate h5payGate = payGateService.getPayGateById(h5paygateid);
		PayGate iosbpayGate = payGateService.getPayGateById(iospaygateid);
		PayGate androidpayGate = payGateService
				.getPayGateById(androidpaygateid);

		bankInfo.setWebPaygate(webpayGate);
		bankInfo.setH5Paygate(h5payGate);
		bankInfo.setiOSPaygate(iosbpayGate);
		bankInfo.setAndroidPaygate(androidpayGate);

		if (bankInfoService.saveBankInfo(bankInfo)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "银行添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "银行添加失败");
		}

		return "redirect:/bankmanage/bankmanagelist";
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "bankmanageupdate/{id}", method = RequestMethod.GET)
	public String updateBankManage(@PathVariable Long id, ModelMap model) {
		BankInfo bankInfo = bankInfoService.getBankInfoById(id);
		List<PayGate> pgList = payGateService.getPayGateList();
		model.put("pgList", pgList);
		model.put("bankManage", bankInfo);
		return "/setting/bankmanage_input";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "bankmanageupdate/{id}", method = RequestMethod.POST)
	public String saveUpdateBankManage(@PathVariable Long id,
			BankInfo bankInfo, Long webpaygateid, Long h5paygateid,
			Long iospaygateid, Long androidpaygateid,
			RedirectAttributes redirectAttributes) {

		PayGate webpayGate = payGateService.getPayGateById(webpaygateid);
		PayGate h5payGate = payGateService.getPayGateById(h5paygateid);
		PayGate iosbpayGate = payGateService.getPayGateById(iospaygateid);
		PayGate androidpayGate = payGateService
				.getPayGateById(androidpaygateid);

		bankInfo.setWebPaygate(webpayGate);
		bankInfo.setH5Paygate(h5payGate);
		bankInfo.setiOSPaygate(iosbpayGate);
		bankInfo.setAndroidPaygate(androidpayGate);

		if (bankInfoService.updateBankInfo(bankInfo)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "银行修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "银行修改失败");
		}

		return "redirect:/bankmanage/bankmanagelist";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "bankmanagedelete/{id}", method = RequestMethod.GET)
	public String deleteBankManage(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (bankInfoService.deleteBankInfo(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "银行删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "银行删除失败");
		}

		return "redirect:/bankmanage/bankmanagelist";
	}

}
